package com.xxrl.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.xxrl.shop.common.entity.JsonResult;
import com.xxrl.shop.common.utils.ResultTool;
import com.xxrl.shop.config.log.ControllerLog;
import com.xxrl.shop.service.OrderService;
import com.xxrl.shop.service.ProductService;
import com.xxrl.shop.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = {"statistics"})
@RestController
@RequestMapping("/statist")
public class StatisticsController {

    private final OrderService orderService;
    private final UserService userService;
    private final ProductService productService;


    public StatisticsController(OrderService orderService, UserService userService, ProductService productService) {
        this.orderService = orderService;
        this.userService = userService;
        this.productService = productService;
    }

    @GetMapping("dashboard")
    @Cacheable(cacheNames = "homePageStatistics")
    @ControllerLog
    public JsonResult<String> homePageCount() {
        JSONObject json = new JSONObject();
        json.put("visit", productService.countVisit());
        json.put("product", productService.countProduct());
        json.put("user", userService.countUser());
        json.put("order", orderService.countOrder());
        return ResultTool.success(json.toString());
    }

}
